package top.humbleyuan.string;

/**
 * @Author: yuanhuanghui
 * @Date: 2020-07-30
 * @Time: 10:39
 * @Description: 字符串最长公共前缀
 * @pro: 待补充新方法（二分法，分治法）
 */
public class LeetCode_14 {

    // 普通的字符串遍历，注意字符串为null或者""的判断
    public static void main(String[] args) {
        //String[] strs = {"flower", "flow", "flight"};
        String[] strs = {"", "flow", "flight"};

        if(strs == null || strs.length == 0) {
            System.out.println("无公共部分");
        }

        String common = strs[0] != null ? strs[0] : "";
        for (int i = 1; i < strs.length; i++) {
            int j = 0;

            String curStr = strs[i] != null ? strs[i] : "";
            for (j = 0; j < curStr.length(); j++) {
                if(j >= common.length() || curStr.charAt(j) != common.charAt(j)) {
                    break;
                }
            }

            common = common.substring(0, j);
        }

        if(common.equals("")) {
            System.out.println("无公共部分");
            return;
        }
        System.out.println(common);

    }
}
